﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace ConsoleApp1
{
    class Class1
    {

        public static string com = "server=.;database=dba;Integrated Security=true;";



        //查询
        public static SqlDataReader Id(string sql) 
        {
            //创建连接对象
            SqlConnection con = new SqlConnection(com);
            //打开连接
            con.Open();
            //创建对象命令
            SqlCommand select = new SqlCommand(sql,con);
            //ExecuteReader执行
            return select.ExecuteReader(System.Data.CommandBehavior.CloseConnection);


        }


        //方法二  增删改
        public static bool Exe(string sql) 
        {

            bool result = false;
            //创建连接对象
            SqlConnection con = new SqlConnection(com);
            //打开连接
            con.Open();
            //创建对象命令
            SqlCommand select = new SqlCommand(sql, con);

            int number = select.ExecuteNonQuery();

            if (number>0) 
            {
                result = true;
            }

            con.Close();
            return result;

        }

        //方法三  聚合函数
        public static object Ex(string sql) 
        {
            //创建连接对象
            SqlConnection con = new SqlConnection(com);
            //打开连接
            con.Open();
            //创建对象命令
            SqlCommand select = new SqlCommand(sql, con);

            object re = select.ExecuteScalar();
            con.Close();
            return re;

        }
    }
}
